package com.jyy.prefabricated.controller;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.jyy.prefabricated.common.AjaxResult;
import com.jyy.prefabricated.dto.Pageable;
import com.jyy.prefabricated.entity.CompanyAd;
import com.jyy.prefabricated.service.CompanyAdService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;


/**
 * 广告位
 * @version 1.0.0
 * @author: zjj
 */
@Slf4j
@RequestMapping("/api")
@RestController
public class CompanyAdController {

    @Autowired
    private CompanyAdService companyAdService;

    /**
     * 查询企业广告详情
     * @param id
     * @return
     */
    @GetMapping("/company-ad/{id}")
    public AjaxResult selectById(@PathVariable Integer id){
        log.info("Rest request to find companyAd by id:{}",id);
        CompanyAd result = companyAdService.selectById(id);
        return AjaxResult.success(result);
    }

    /**
     * 分页查询企业广告
     * @param position 广告位置
     * @param status 状态
     * @param pageable
     * @return
     */
    @GetMapping("/company-ad")
    public AjaxResult selectByParams(@RequestParam(required = false) Integer position, @RequestParam(required = false) Integer status, Pageable pageable){
        log.info("Rest request to find companyAd by params position:{},status:{}", position, status);
        PageHelper.startPage(pageable.getPageNum(), pageable.getPageSize());
        List<CompanyAd> companyAds = companyAdService.selectByParams(position, status);
        PageInfo<CompanyAd> result = new PageInfo<>(companyAds);
        return AjaxResult.success(result);
    }

    /**
     * 删除广告
     * @param id
     * @return
     */
    @DeleteMapping("/company-ad/{id}")
    public AjaxResult deleteById(@PathVariable Integer id){
        log.info("Rest request to delete companyAd by id:{}",id);
        int row = companyAdService.delete(id);
        if(row > 0){
            return AjaxResult.success();
        } else {
            return AjaxResult.error();
        }
    }


}
